<?php
include 'securities.php';
class Manage_user_permissions extends Securities {
	function __construct() {
		parent::__construct('manage_user_permissions');
		$this -> form_validation -> set_error_delimiters('<span class="error">', '</span>');
	}

	public function index() {
		$data['title'] = $this->lang->line('title_list_user_group');
		$data['controller_name'] = strtolower(get_class());
		$data['manage_datatable'] = get_user_group_manage_table($this -> mod_globals -> select_all(PREFIX_TABLE . 'user_groups'), $this -> uri -> segment(1));
		$this -> load -> view(B_MASTER . 'index', $data);
	}

	public function insert_user_permission() {
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));

		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$data['title'] = $this->lang->line('title_new_user_group');
		$this -> form_validation -> set_rules('user_group_name', 'lang:form_label_user_group_name', 'required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['modules'] = $this -> mod_globals -> select_all(PREFIX_TABLE . "modules");
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {

			$group_name = $this -> input -> post("user_group_name");
			$module_access = $this -> input -> post("module_access");
			$module_modify = $this -> input -> post("module_modify");

			// define array
			$group_access = array();
			$group_modify = array();

			//insert into user groups
			$group_name_last_id = $this -> mod_globals -> insert_get_last_id(PREFIX_TABLE . "user_groups", array('name' => $group_name, 'status' => 1, 'created_date' => null));

			if ($group_name_last_id) {

				foreach ($module_access as $acc_mod_id) {
					array_push($group_access, array($group_name_last_id, $acc_mod_id, 1, null));
				}
				foreach ($module_modify as $mod_mod_id) {
					array_push($group_modify, array($group_name_last_id, $mod_mod_id, 1, null));
				}

				$this -> mod_globals -> insert_multi(PREFIX_TABLE . "group_access_permissions", array('use_user_group_id', 'use_module_id', 'status', 'created_date'), $group_access);
				$this -> mod_globals -> insert_multi(PREFIX_TABLE . "group_modify_permissions", array('use_user_group_id', 'use_module_id', 'status', 'created_date'), $group_modify);

				$this -> session -> set_flashdata('success', 'User  group was created success.');
				redirect('manage-user-permissions');
			} else {

				$this -> session -> set_flashdata('error', 'User group  was created fail.');
				redirect('manage-user-permissions');
			}

		}
	}

	public function update_user_permission() {
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));
		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$data['title'] = $this->lang->line('title_update_user_group');
		$group_id = $this -> uri -> segment(3);

		$this -> form_validation -> set_rules('user_group_name', 'lang:form_label_user_group_name', 'required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['module_access'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "group_access_permissions", array('use_user_group_id' => $group_id));
			$data['module_modify'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "group_modify_permissions", array('use_user_group_id' => $group_id));
			$data['modules'] = $this -> mod_globals -> select_all(PREFIX_TABLE . "modules");
			$data['group_name'] = $this -> mod_globals -> select_string(PREFIX_TABLE . "user_groups", "name", array('user_group_id' => $group_id));
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {

			$group_name = $this -> input -> post("user_group_name");
			$module_access = $this -> input -> post("module_access");
			$module_modify = $this -> input -> post("module_modify");

			// define array
			$group_access = array();
			$group_modify = array();

			//insert into user groups
			$this -> mod_globals -> update(PREFIX_TABLE . "user_groups", array('name' => $group_name), array('user_group_id' => $group_id));

			$this -> mod_globals -> delete(PREFIX_TABLE . "group_access_permissions", array('use_user_group_id' => $group_id));
			$this -> mod_globals -> delete(PREFIX_TABLE . "group_modify_permissions", array('use_user_group_id' => $group_id));

			foreach ($module_access as $acc_mod_id) {
				array_push($group_access, array($group_id, $acc_mod_id, 1, null));
			}
			foreach ($module_modify as $mod_mod_id) {
				array_push($group_modify, array($group_id, $mod_mod_id, 1, null));
			}

			$this -> mod_globals -> insert_multi(PREFIX_TABLE . "group_access_permissions", array('use_user_group_id', 'use_module_id', 'status', 'created_date'), $group_access);
			$this -> mod_globals -> insert_multi(PREFIX_TABLE . "group_modify_permissions", array('use_user_group_id', 'use_module_id', 'status', 'created_date'), $group_modify);

			$this -> session -> set_flashdata('success', 'User  group was update success.');
			redirect('manage-user-permissions');

		}
	}

	public function delete_user_permission() {

		$module_id = str_replace('-', '_', $this -> uri -> segment(1));

		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$group_id = $this -> uri -> segment(3);

		$whe_delete_user_group = array('user_group_id' => $group_id);
		$is_deleted = $this -> mod_globals -> delete(PREFIX_TABLE . 'user_groups', $whe_delete_user_group);
		if ($is_deleted) {
			$this -> session -> set_flashdata('success', 'User group data was deleted success.');
			redirect('manage-user-permissions');
		} else {
			$this -> session -> set_flashdata('error', 'User data was deleted fail.');
			redirect('manage-user-permissions');
		}
	}

	public function user_modules() {
		$module_id = str_replace('-', '_', $this -> uri -> segment(1));

		if (!$this -> is_has_modify_permission($module_id)) {
			redirect('ctm-common/no-allow-modify/' . $module_id);
		}

		$data['title'] = "Update User Group";
		$user_id = $this -> uri -> segment(3);

		$group_id = $this -> mod_globals -> select_string(PREFIX_TABLE . "users", "use_user_group_id", array('user_id' => $user_id));

		$this -> form_validation -> set_rules('user_group_name', 'lang:form_label_user_group_name', 'required');

		if ($this -> form_validation -> run() === FALSE) {
			$data['module_access'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "group_access_permissions", array('use_user_group_id' => $group_id));
			$data['module_modify'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "group_modify_permissions", array('use_user_group_id' => $group_id));

			$data['user_module_access'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "user_access_permissions", array('use_user_id' => $user_id));
			$data['user_module_modify'] = $this -> mod_globals -> select_where(PREFIX_TABLE . "user_modify_permissions", array('use_user_id' => $user_id));

			$data['modules'] = $this -> mod_globals -> select_all(PREFIX_TABLE . "modules");
			$data['group_name'] = $this -> mod_globals -> select_string(PREFIX_TABLE . "user_groups", "name", array('user_group_id' => $group_id));
			$this -> load -> view(B_MASTER . 'index', $data);
		} else {

			//$group_name = $this -> input -> post("user_group_name");
			$module_access = $this -> input -> post("module_access");
			$module_modify = $this -> input -> post("module_modify");

			// define array
			$user_access = array();
			$user_modify = array();

			// pass module to define array
			foreach ($module_access as $acc_mod_id) {
				if ($this -> mod_globals -> select_where(PREFIX_TABLE . "group_access_permissions", array('use_user_group_id' => $group_id, 'use_module_id' => $acc_mod_id)) -> num_rows() <= 0) {
					array_push($user_access, array($user_id, $acc_mod_id, 1, null));
				}
			}
			foreach ($module_modify as $mod_mod_id) {
				if ($this -> mod_globals -> select_where(PREFIX_TABLE . "group_modify_permissions", array('use_user_group_id' => $group_id, 'use_module_id' => $mod_mod_id)) -> num_rows() <= 0) {
					array_push($user_modify, array($user_id, $mod_mod_id, 1, null));
				}
			}
			// check if module exits in recored,delete
			if ($this -> mod_globals -> select_where(PREFIX_TABLE . "user_access_permissions", array('use_user_id' => $user_id)) -> num_rows() > 0) {
				$this -> mod_globals -> delete(PREFIX_TABLE . "user_access_permissions", array('use_user_id' => $user_id));
			}

			if ($this -> mod_globals -> select_where(PREFIX_TABLE . "user_modify_permissions", array('use_user_id' => $user_id)) -> num_rows() > 0) {
				$this -> mod_globals -> delete(PREFIX_TABLE . "user_modify_permissions", array('use_user_id' => $user_id));
			}

			// insert module to user

			$this -> mod_globals -> insert_multi(PREFIX_TABLE . "user_access_permissions", array('use_user_id', 'use_module_id', 'status', 'created_date'), $user_access);
			$this -> mod_globals -> insert_multi(PREFIX_TABLE . "user_modify_permissions", array('use_user_id', 'use_module_id', 'status', 'created_date'), $user_modify);

			$this -> session -> set_flashdata('success', 'User  module was modified success.');
			redirect('manage-users');

		}

	}

}
